<script setup lang="ts">
  import { getCodeAPI, loginByCodeAPI } from '@/api/user'
  import { useUserStore } from '@/stores/user'
  import { ref } from 'vue'

  defineOptions({
    options: {
      styleIsolation: 'shared',
    },
  })
  //登陆手机号，验证码
  const loginForm = ref({
    mobile: '13230000001',
    code: '',
  })
  //勾选协议
  const isChecked = ref(false)
  //定义UI-form的实例对象
  const formRef = ref<UniHelper.UniFormsInstance>()
  // 验证表单数据的规则
  const formRules = {
    mobile: {
      rules: [
        { required: true, errorMessage: '请填写手机号码' },
        { pattern: /^1\d{10}$/, errorMessage: '手机号码格式不正确' },
      ],
    },
    code: {
      rules: [
        { required: true, errorMessage: '请输入验证码' },
        { pattern: /^\d{6}$/, errorMessage: '验证码格式不正确' },
      ],
    },
  }
  const showCutDown = ref(false)
  // 点击获取验证码
  const changeCutDown = async () => {
    try {
      await formRef.value!.validateField!(['mobile'])
      showCutDown.value = true
    } catch (error) {
      uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
    }
    const res = await getCodeAPI({
      mobile: loginForm.value.mobile,
      type: 'login',
    })
    loginForm.value.code = res.code
  }
  // 点击登录
  const onSubmit = async () => {
    if (!isChecked.value) {
      return uni.showToast({ title: '请勾选协议', icon: 'none' })
    }
    try {
      await formRef.value!.validate!()
    } catch (error) {
      uni.showToast({ title: '请输入正确的手机号或验证码', icon: 'none' })
    }
    const res = await loginByCodeAPI(loginForm.value)
    const store = useUserStore()
    store.token = res.token
    store.userId = res.id!
    uni.showToast({ title: '登陆成功' })
    uni.switchTab({ url: '/pages/index/index' })
  }
</script>

<template>
  <uni-forms :model="loginForm" ref="formRef" :rules="formRules" class="login-form">
    <uni-forms-item name="mobile">
      <uni-easyinput
        v-model="loginForm.mobile"
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        v-model="loginForm.code"
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
      />
      <text v-if="showCutDown === false" class="text-button" @click="changeCutDown"
        >获取验证码</text
      >
      <view v-else class="text-button">
        <uni-countdown
          :show-day="false"
          :showMinute="false"
          :showHour="false"
          :second="59"
          color="#16c2a3"
        />秒后重新获取
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isChecked" color="#16C2A3" @click="isChecked = !isChecked" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onSubmit">登 录</button>
  </uni-forms>
</template>

<style lang="scss">
  @import './styles.scss';
</style>
